//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.redis;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.yaukie.base.config.MapTemplate;
import org.yaukie.base.text.Convert;
import org.yaukie.base.util.StringTools;

@Component
public class RedisOrMapCache {
    private static final Logger log = LoggerFactory.getLogger(RedisOrMapCache.class);
    @Value("${spring.redis.enabled:false}")
    private String enabled;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MapTemplate mapTemplate;

    public RedisOrMapCache() {
    }

    public <V> void setCacheObject(final String key, final V value) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            this.redisTemplate.opsForValue().set(key, value);
        } else {
            this.printCacheDes(key);
            this.mapTemplate.setCacheObject(key, value);
        }

    }

    public <V> void setCacheObject(final String key, final V value, final Integer timeout, final TimeUnit timeUnit) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            this.redisTemplate.opsForValue().set(key, value, (long)timeout, timeUnit);
        } else {
            this.printCacheDes(key);
            this.mapTemplate.setCacheObject(key, value, timeout, timeUnit);
        }

    }

    public boolean expire(final String key, final long timeout) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.expire(key, timeout, TimeUnit.SECONDS);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.expire(key, timeout, TimeUnit.SECONDS);
        }
    }

    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.expire(key, timeout, unit);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.expire(key, timeout, unit);
        }
    }

    public <V> V getCacheObject(final String key) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            ValueOperations<String, V> operation = this.redisTemplate.opsForValue();
            return operation.get(key);
        } else {
            this.printCacheDes(key);
            return (V) this.mapTemplate.getCacheObject(key);
        }
    }

    public boolean deleteObject(final String key) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.delete(key);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.deleteObject(key);
        }
    }

    public long deleteObject(final Collection collection) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.delete(collection);
        } else {
            this.printCacheDes(collection.toArray().toString());
            return this.mapTemplate.deleteObject(collection);
        }
    }

    public <V> long setCacheList(final String key, final List<V> dataList) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            Long count = this.redisTemplate.opsForList().rightPushAll(key, dataList);
            return count == null ? 0L : count;
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.setCacheList(key, dataList);
        }
    }

    public <V> List<V> getCacheList(final String key) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.opsForList().range(key, 0L, -1L);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.getCacheList(key);
        }
    }

/*    public <V> BoundSetOperations<String, V> setCacheSet(final String key, final Set<V> dataSet) {
        BoundSetOperations<String, V> setOperation = this.redisTemplate.boundSetOps(key);
        Iterator<V> it = dataSet.iterator();

        while(it.hasNext()) {
            setOperation.add(new Object[]{it.next()});
        }

        return setOperation;
    }*/

    public <V> Set<V> getCacheSet(final String key) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.opsForSet().members(key);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.getCacheSet(key);
        }
    }

    public <V> void setCacheMap(final String key, final Map<String, V> dataMap) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            if (dataMap != null) {
                this.redisTemplate.opsForHash().putAll(key, dataMap);
            }
        } else {
            this.printCacheDes(key);
            this.mapTemplate.setCacheMap(key, dataMap);
        }

    }

    public <V> Map<String, V> getCacheMap(final String key) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.opsForHash().entries(key);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.getCacheMap(key);
        }
    }

    public <V> void setCacheMapValue(final String key, final String hKey, final V value) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            this.redisTemplate.opsForHash().put(key, hKey, value);
        } else {
            this.printCacheDes(key);
            this.mapTemplate.setCacheMapValue(key, hKey, value);
        }

    }

/*    public <V> V getCacheMapValue(final String key, final String hKey) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            HashOperations<String, String, V> opsForHash = this.redisTemplate.opsForHash();
            return opsForHash.get(key, hKey);
        } else {
            this.printCacheDes(key);
            return this.mapTemplate.getCacheMapValue(key, hKey);
        }
    }*/

    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
        return this.redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    public Collection<String> keys(final String pattern) {
        if (!StringTools.isEmpty(this.enabled) && Convert.toBool(this.enabled)) {
            return this.redisTemplate.keys(pattern);
        } else {
            this.printCacheDes(pattern);
            return this.mapTemplate.keys(pattern);
        }
    }

    private void printCacheDes(String key) {
        if (log.isInfoEnabled()) {
            log.info("当前系统使用MAP作为缓存,正在操作Key==[{}]==", key);
        }

    }
}
